package utils;

import javax.swing.*;
import java.util.Scanner;

public class ValidationUtil {
    /*
     * 获取输入字符串
     *
     * @return 字符串sc.next();
     */
    public static String getString() {
        @SuppressWarnings("resource")
        Scanner sc = new Scanner(System.in);
        return sc.next();
    }

    /*
     * 创建密码，带有重复验证功能
     *
     * @return 返回密码
     */
    public static String getPassword(String password, String apassword) {
        if (!password.equals(apassword)) {
            JOptionPane.showMessageDialog(null, "两次密码不一样，请重新输入！");
            return null;
        } else {
            return apassword;
        }
    }


    public static int getNum(int start, int end) {
        Scanner sc = new Scanner(System.in);
        int num = 0;
        while (true) {
            while (true) {
                String str = sc.next();
                try {
                    num = Integer.parseInt(str);
                } catch (NumberFormatException e) {
                    System.out.println("输入的不是数字！请重新输入");
                    continue;
                }
                break;
            }
            if (num < start || num > end) {
                System.out.println("无效操作，输入数字必须在" + start + "与" + end + "之间，请重新输入：");
                continue;
            }
            break;//拿到正确的区间值
        }
        return num;
    }

    /*
     * 获取无范围浮点数
     *
     * @return 输入的浮点数
     */
    public static double get(String str) {
        double money_deposit = 0;
        while (true) {
            try {
                money_deposit = Double.parseDouble(str);
            } catch (NumberFormatException e2) {
                JOptionPane.showMessageDialog(null, "输入的不是数字！请重新输入!");
                continue;
            }
            break;
        }
        return money_deposit;
    }
}


















